package vn.orderonline.web.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.orderonline.com.core.business.admin.IAdmin;
import vn.orderonline.com.core.business.admin.IRole;
import vn.orderonline.com.core.business.common.ICommon;
import vn.orderonline.com.core.business.dto.AdminDto;
import vn.orderonline.com.core.business.dto.DistrictDto;
import vn.orderonline.core.commons.utils.JsonUtils;
import vn.orderonline.core.commons.utils.StringUtils;

@Controller
@RequestMapping("/admin")
public class AdminController {

	private static final Logger LOGGER = Logger
			.getLogger(AdminController.class);
	
	@Autowired
	private IAdmin iAdmin;
	@Autowired
	private IRole iRole;
	@Autowired
	private ICommon iCommon;


	@RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String loginAdminPage(ModelMap model) {
		LOGGER.info("START TO SEARCH ADMIN");
		try {
			model.addAttribute("role", iRole.getAllRoles());
			model.addAttribute("city", iCommon.cityList());
			model.addAttribute("list", iAdmin.getAllAdmin());
		} catch (Exception e) {
			LOGGER.info(e.getMessage());
		}
		LOGGER.info("END TO SEARCH ADMIN!");
		return "/admin/search-admin";
	}
	
	@RequestMapping(value = "/getdistrictbycityid", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody public String getDistrictByCityId(@RequestParam Integer cityId) {
		LOGGER.info("START TO GET DISTRICT BY CITYID! " + cityId);
		List<DistrictDto> list = null;
		String str = null;
		try {
			list = iCommon.districtList(cityId);
			str = StringUtils.parseToJSON(list);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO GET DISTRICT BY CITYID!");
		return str;
	}
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String detail(ModelMap model, @RequestParam String adminLoginId) {
		LOGGER.info("START TO VIEW DETAIL ADMIN!");
		AdminDto adminDto = null;
		try {
			adminDto = iAdmin.getAdminByAdminLoginId(adminLoginId);
			System.out.println("value1: " + JsonUtils.toJson(adminDto));
			model.addAttribute("admin", adminDto);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			e.printStackTrace();
		}
		LOGGER.info("END TO VIEW DETAIL ADMIN!");
		return "/admin/detail-admin";
	}
	
	@RequestMapping(value = "/form", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String formAdd(ModelMap model) {
		LOGGER.info("START TO CREATE NEW FORM ADD ADMIN!");
		try {
			//model.addAttribute("admin", new AdminDto());
			model.addAttribute("city", iCommon.cityList());
			model.addAttribute("role", iRole.getAllRoles());
			model.addAttribute("admin", new AdminDto());
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO CREATE NEW FORM ADD ADMIN!");
		return "/admin/add-admin";
		
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public String addAdmin(@ModelAttribute AdminDto adminDto) {
		LOGGER.info("START TO ADD NEW ADMIN! " + adminDto);
		try {
			//iAdmin.add(adminDto);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO ADD NEW ADMIN!");
		return "redirect:/admin/search";
	}

	@RequestMapping(value = "/checkexistedemail", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody public String checkExistedEmail(@RequestParam String adminEmail) {
		LOGGER.info("START TO CHECK EXISTED EMAIL! " + adminEmail);
		Boolean existed = false;
		try {
			existed = iAdmin.checkExistedByEmail(adminEmail);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO CHECK EXISTED EMAIL!");
		return existed.toString();
	}
	
	@RequestMapping(value = "/checkexistedadminloginid", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody public String checkExistedAdminLoginId(@RequestParam String adminLoginId) {
		LOGGER.info("START TO CHECK EXISTED ADMINLOGINID! " + adminLoginId);
		Boolean existed = false;
		try {
			existed = iAdmin.checkExistedByAdminLoginId(adminLoginId);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO CHECK EXISTED ADMINLOGINID! " + existed);
		return existed.toString();
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String deleteAdmin(@RequestParam String adminLoginId) {
		LOGGER.info("START TO DELETE ADMIN! " + adminLoginId);
		try {
			iAdmin.delete(adminLoginId);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		LOGGER.info("END TO DELETE ADMIN!");
		return "redirect:/admin/search";
	}
//
//	@RequestMapping(value = "/edit", method = RequestMethod.GET)
//	public String editAdmin(@RequestParam Integer adminId, ModelMap model) {
//
//		LOGGER.info("DA VAO CONTROLLER EDIT ADMIN");
//		LOGGER.info("ADMIN ID : " + adminId);
//
//		try {
//			model.addAttribute("admin", iAdminManager.getAdminByID(adminId));
//			model.addAttribute("listRole", iRole.getAllRoleMap());
//		} catch (Exception e) {
//			LOGGER.info("ERROR WHEN GET ADMIN EDIT DATA ! ", e);
//		}
//		return "/admin/AdminEdit";
//	}
//
//	@RequestMapping(value = "/update")
//	public String updateAdmin(@RequestParam Integer adminId,
//			@ModelAttribute AdminDto adminDto, Principal principal) {
//
//		LOGGER.info("DA VAO CONTROLLER UPDATE ADMIN");
//		LOGGER.info("ADMINID : " + adminId);
//
//		try {
//			adminDto.setUpdateId(principal.getName());
//			iAdminManager.update(adminDto);
//		} catch (Exception e) {
//			LOGGER.info("ERROR WHEN UPDATE ADMIN ! ", e);
//		}
//		return "redirect:/admin/list";
//	}

}